<template>
  <!-- model-value控制对话框显示与否的绑定值 -->
  <el-dialog
    model-value="dialogVisible"
    :title="dialogTitle"
    width="30%"
  @close="handleClose"
  >
  <!-- Element Plus的Form组件，用于创建表单 -->  
  <!-- rules表单验证规则 -->  
    <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
    >
    
      <el-form-item label="上级菜单" prop="parentId">
        <el-select v-model="form.parentId" placeholder="请选择上级菜单" @change="$forceUpdate()">
          <template v-for="item in tableData">
            <el-option :label="item.name" :value="item.id"></el-option>
            <template v-for="child in item.children">
              <el-option :label="child.name" :value="child.id">
                <span>{{ "    -- " + child.name }}</span>
              </el-option>
            </template>
          </template>
        </el-select>
      </el-form-item>

      <el-form-item label="菜单类型" prop="menuType" label-width="100px">
     <el-radio-group v-model="form.menuType">
      <el-radio :label="'M'">目录</el-radio>
      <el-radio :label="'C'">菜单</el-radio>
      <el-radio :label="'F'">按钮</el-radio>
     </el-radio-group>
    </el-form-item>

      <el-form-item label="菜单图标" prop="icon">
        <el-input v-model="form.icon" />
      </el-form-item>
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="perms">
        <el-input v-model="form.perms" />
      </el-form-item>
      <el-form-item label="组件路径" prop="component">
        <el-input v-model="form.component" />
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum" >
     <el-input-number v-model="form.orderNum" :min="1" label="显示顺序"></el-input-number>
    </el-form-item>
    </el-form>
    <!-- 自定义对话框底部 -->  
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button  @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
 </template>
 <script setup>
 import {defineEmits, defineProps, ref, watch} from "vue";
 import requestUtil,{getServerUrl} from "@/util/request";
 import { ElMessage } from 'element-plus'
  const tableData=ref([])
  /*
        defineProps是Vue3的编译时宏函数,
        用于接收父组件向子组件传递的属性(props)
    */
   // 定义接收的父组件属性 
  const props=defineProps(
      {
        id:{//菜单id
          type:Number,
          default:-1,
          required:true
        },
        dialogTitle:{//对话框标题
          type:String,
          default:'',
          required:true
        },
        dialogVisible:{//对话框显示状态
          type:Boolean,
          default:false,
          required:true
        },
        tableData:{// 上级菜单数据
          type:Array,
          default:[],
          required:true
        }
      }
  )
  // 表单数据模型
 const form=ref({
  id:-1,
  parentId:'',
  menuType:"M",
  icon:'',
  name:'',
  perms:'',
  component:'',
  orderNum:1
 })
 // 表单验证规则  
 const rules=ref({
  parentId:[
    { required: true, message: '请选择上级菜单'}
  ],
  name: [{ required: true, message: "菜单名称不能为空", trigger: "blur" }]
 })
 const formRef=ref(null)
 const initFormData=async(id)=>{
 const res=await requestUtil.get("sys/menu/"+id);
 form.value=res.data.sysMenu;// 将获取的数据赋值给表单模型  
 }
 // 监听对话框显示状态的变化 
  watch(
      ()=>props.dialogVisible,
      ()=>{
        let id=props.id;
        tableData.value=props.tableData;// 更新上级菜单数据 
        if(id!=-1){
          initFormData(id)
        }else{
          form.value={
            id:-1,
            parentId:'',
            menuType:"M",
            icon:'',
            name:'',
            perms:'',
            component:'',
            orderNum:1
          }
        }
      }
  )
  // 关闭对话框
const handleClose=()=>{
 emits('update:modelValue',false)// 通知父组件对话框已关闭  
 }
 // 定义可触发的事件 
 const emits=defineEmits(['update:modelValue','initMenuList'])
 // 确认提交表单 
 const handleConfirm=()=>{
 formRef.value.validate(async(valid)=>{
 if(valid){//如果表单验证通过
 let result=await requestUtil.post("sys/menu/save",form.value);// 发送请求保存菜单数据  
 let data=result.data;
 if(data.code==200){
 ElMessage.success("执行成功！")
 formRef.value.resetFields();//重置表单
 emits("initMenuList")// 通知父组件重新加载菜单列表  
 handleClose();
 }else{
 ElMessage.error(data.msg);//显示错误信息
 }
 }else{
 console.log("fail")
 }
 })
 }

 </script>
 <style scoped>
 </style>
